package org.dromara.system.controller;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.springdoc.core.annotations.ParameterObject;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysBillRuleVo;
import org.dromara.system.domain.bo.SysBillRuleBo;
import org.dromara.system.service.ISysBillRuleService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 单据规则
 *
 * @author yilong.fu
 * @date 2024-11-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/system/billRule")
public class SysBillRuleController extends BaseController {

    private final ISysBillRuleService sysBillRuleService;

    /**
     * 查询单据规则列表
     */
    @SaCheckPermission("system:billRule:list")
    @GetMapping("/list")
    public TableDataInfo<SysBillRuleVo> list(@ParameterObject SysBillRuleBo bo,@ParameterObject PageQuery pageQuery) {
        return sysBillRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出单据规则列表
     */
    @SaCheckPermission("system:billRule:export")
    @Log(title = "单据规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ParameterObject SysBillRuleBo bo, HttpServletResponse response) {
        List<SysBillRuleVo> list = sysBillRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "单据规则", SysBillRuleVo.class, response);
    }

    /**
     * 获取单据规则详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:billRule:query")
    @GetMapping("/{id}")
    public R<SysBillRuleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysBillRuleService.queryById(id));
    }

    /**
     * 新增单据规则
     */
    @SaCheckPermission("system:billRule:add")
    @Log(title = "单据规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody SysBillRuleBo bo) {
        boolean flag = sysBillRuleService.insertByBo(bo);
        if (!flag) {
            return R.fail();
        }
        return R.ok(bo.getId());
    }

    /**
     * 修改单据规则
     */
    @SaCheckPermission("system:billRule:edit")
    @Log(title = "单据规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysBillRuleBo bo) {
        return toAjax(sysBillRuleService.updateByBo(bo));
    }

    /**
     * 删除单据规则
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:billRule:remove")
    @Log(title = "单据规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysBillRuleService.deleteWithValidByIds(List.of(ids), true));
    }
}
